//
//  TNLanuageTableViewController.m
//  TravelNotes
//
//  Created by 黄锋 on 15/8/23.
//  Copyright (c) 2015年 王浩宇. All rights reserved.
//

#import "TNLanuageTableViewController.h"

@interface TNLanuageTableViewController ()

@property(nonatomic) NSInteger index;

@end

@implementation TNLanuageTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _index = 0;
    for (NSString *str in _lanuageArray) {
        if ([str isEqualToString:_string]) {
            break;
        }
        _index++;
    }
//    NSLog(@"string:%@, index:%ld", _string, _index);
    if (_index >= _lanuageArray.count) {
        _index = 1;
    }
    [self.tableView reloadData];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _lanuageArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = _lanuageArray[indexPath.row];
    if (_index == indexPath.row) {
        [cell.imageView setImage:[UIImage imageNamed:@"GCImagePickerControllerCheckGreen"]];
    } else {
        [cell.imageView setImage:[UIImage imageNamed:@""]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_choseBlock) {
        _choseBlock(_lanuageArray[indexPath.row]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
